package model

import (
	"stock/global"
	"sync"
)

type NewsArticleModel struct {
	Base
}

type SysNewsArticle struct {
	Id              uint   `json:"id" gorm:"<-:false; primaryKey;column:id;type:int(11)"`
	MerchantId      uint   `json:"merchant_id" gorm:"<-:false; column:merchant_id;"`
	UserId          uint   `json:"user_id" gorm:"<-:false; column:user_id;"`
	ArticleType     string `json:"article_type" gorm:"column:article_type;"`
	Cover           string `json:"cover" gorm:"column:cover;"`
	Path            string `json:"path" gorm:"column:path;"`
	CreateTime      uint   `json:"create_time" gorm:"column:create_time;"`
	CheckState      uint8  `json:"check_state" gorm:"column:check_state;"`
	Status          uint8  `json:"status" gorm:"column:status;"`
	IsRecommend     uint8  `json:"is_recommend" gorm:"column:is_recommend;"`
	Perfect         string `json:"perfect" gorm:"column:perfect;"`
	NewsArticleLang string `json:"lang" gorm:"column:lang;"`
}

type RequestSysNewsArticle struct {
	MerchantId      uint
	ArticleType     string
	CheckState      uint8
	IsRecommend     uint8
	Perfect         string
	NewsArticleLang uint8
	Status          uint8
	IdArr           []uint
}

type ResponseSysNewsArticle struct {
	SysNewsArticle
	ResponseLang
}

func (m *NewsArticleModel) TabName() string {
	return global.CONF.Mysql.Prefix + "sys_news_article"
}

// 列表
func (m *NewsArticleModel) List(param *RequestSysNewsArticle, page global.PageSort) ([]ResponseSysNewsArticle, error) {

	where := &SysNewsArticle{}
	if param.MerchantId != 0 {
		where.MerchantId = param.MerchantId
	}
	if param.ArticleType != "" {
		where.ArticleType = param.ArticleType
	}
	if param.CheckState != 0 {
		where.CheckState = param.CheckState
	}
	if param.Status != 0 {
		where.Status = param.Status
	}
	var newsArticle []SysNewsArticle
	tx := global.DB.Debug().Where(where).Order("id DESC").Offset(page.Page).Limit(page.Limit).Find(&newsArticle)
	if tx.Error != nil {
		return nil, tx.Error
	}
	newsArticleLen := len(newsArticle)
	var newsArticleArr []ResponseSysNewsArticle
	if newsArticleLen > 0 {
		var newsArticleLang *LangModel = new(LangModel)

		var newsArticleWg sync.WaitGroup
		newsArticleWg.Add(newsArticleLen)
		for i := 0; i < newsArticleLen; i++ {
			go func(tabName string, tableId uint, newsArt SysNewsArticle, wg *sync.WaitGroup) {

				var newsArticleRes ResponseLang
				var resNewsArticle ResponseSysNewsArticle
				newsArticleRes, _ = newsArticleLang.List(RequestLang{
					TableName: tabName,
					TableId:   tableId,
					Lang:      global.LANG_COUNTRY.Lang,
					Field:     []string{"introduction", "news_article_content", "title"},
				})
				resNewsArticle.Id = newsArt.Id
				resNewsArticle.Title = newsArticleRes.Title
				resNewsArticle.Introduction = newsArticleRes.Introduction
				resNewsArticle.NewsArticleContent = newsArticleRes.NewsArticleContent
				resNewsArticle.Content = newsArticleRes.Content
				resNewsArticle.Cover = newsArt.Cover
				newsArticleArr = append(newsArticleArr, resNewsArticle)
				wg.Done()

			}(m.TabName(), newsArticle[i].Id, newsArticle[i], &newsArticleWg)
		}
		newsArticleWg.Wait()
	}
	return newsArticleArr, nil
}

// 详情
func (m *NewsArticleModel) Detail() {

}

// 基础详情
func (m *NewsArticleModel) BaseDetail() {

}
